package com.sh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sh.mapper.ArticleMapper;
import com.sh.pojo.Article;
import com.sh.pojo.ArticlePageDto;
import com.sh.pojo.PageBean;
import com.sh.service.ArticleService;
import com.sh.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        Map m = ThreadLocalUtil.get();
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setCreateUser((Integer) m.get("id"));
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(ArticlePageDto articlePageDto) {
        Map m=ThreadLocalUtil.get();
        articlePageDto.setCreateUser((Integer) m.get("id"));
        //引用分页插件
        PageHelper.startPage(articlePageDto.getPageNum(),articlePageDto.getPageSize());
        Page<Article>page=articleMapper.list(articlePageDto);//获取集合;
        long total=page.getTotal();
        List<Article>records=page.getResult();//查询道的集合
        return new PageBean<>(total,records);
    }

    @Override
    public Article detail(Integer id) {
        Article article = articleMapper.detail(id);
        return article;
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }
}
